package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.LabelOptionDTO;
import com.jyh.blog.dto.MenuDTO;
import com.jyh.blog.dto.UserMenuDTO;
import com.jyh.blog.service.MenuService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.ConditionVO;
import com.jyh.blog.vo.MenuHiddenVO;
import com.jyh.blog.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.*;

@Api(tags = "菜单管理")
@RestController
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 获取菜单列表
     * @param conditionVO 不做分页，只有keywords有值
     * @return
     */
    @ApiOperation(value = "获取菜单列表")
    @GetMapping("admin/menus")
    public Result<List<MenuDTO>> menuList(ConditionVO conditionVO){
        return Result.ok(menuService.menuList(conditionVO));
    }

    /**
     * 添加或修改菜单
     * @param menuVO
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改菜单")
    @PostMapping("admin/menus")
    public Result<String> saveOrUpdateMenu(@Valid @RequestBody MenuVO menuVO){
        //log.info("要添加的菜单对象:{}",menuVO);
        menuService.saveOrUpdateMenu(menuVO);
        return Result.ok();
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "通过id删除菜单")
    @DeleteMapping("admin/menus/{menuId}")
    public Result<String> deleteMenuById(@PathVariable("menuId") Integer menuId){
        //log.info("要删除的菜单id:{}",menuId);
        menuService.deleteMenuById(menuId);
        return Result.ok();
    }

    /**
     * 菜单是否隐藏
     * @param menuHiddenVO
     * @return
     */
    @OptLog(optType = UPDATE)
    @ApiOperation(value = "隐藏或显示菜单")
    @PutMapping("admin/menus")
    public Result<String> changHidden(@Valid @RequestBody MenuHiddenVO menuHiddenVO){
        menuService.changHidden(menuHiddenVO);
        return Result.ok();
    }

    /**
     * 获取角色菜单列表
     * 权限管理中用
     * @return
     */
    @ApiOperation(value = "获取角色菜单列表")
    @GetMapping("admin/menus/roles")
    public Result<List<LabelOptionDTO>> roleMenuList(){
        return Result.ok(menuService.roleMenuList());
    }


    /**
     * 当前用户菜单
     * @return
     */
    @ApiOperation(value = "当前用户菜单")
    @GetMapping("admin/users/menus")
    public Result<List<UserMenuDTO>> userMenuList(){
        return Result.ok(menuService.userMenuList());
    }
}
